package com.mp.config;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 *
 * @author:
 * @date:
 */
 @Configuration
 @MapperScan(value={"com.mp.*.mapper"})
public class MybatisPlusConfig {

    /**
     * 注册乐观锁插件
     * @return OptimisticLockerInterceptor
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }

    /**
     * 注册分页插件
     * @return PaginationInterceptor
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    /**
     * 注册逻辑删除组件
     * @return ISqlInjector
     */
    @Bean
    public ISqlInjector iSqInjector(){
        return new LogicSqlInjector();
    }

    /**
     * 注册 sql执行效率插件，开发环境有效,performanceInterceptor才会生效
     * @return ISqlInjector
     */
    @Bean
    @Profile(value={"dev","test"})
    public PerformanceInterceptor performanceInterceptor(){
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        //设置sql执行的最大时间，如果超过了则不执行
        performanceInterceptor.setMaxTime(10000);
        //sql格式化
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }



}
